package cn.com.blueInfo.utils.result;

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.*;

/**
 * @Description: TODO
 * @Description:
 * @BelongsProject: springboot2-parent
 * @BelongsPackage: cn.com.blueInfo.framework.result
 * @Author: suxch
 * @CreateTime: 2024/8/15 17:11
 * @Version: 1.0
 */
@Data
@ToString
@NoArgsConstructor
@AllArgsConstructor
@Builder
@Schema(description = "返回前端对象")
public class ResultInfo<T> {

    @Schema(description = "HTTP状态码")
    private Integer code = 200;
    /** 返回的状态 */
    @Schema(description = "业务请求状态")
    private boolean result = true;
    /** 返回的消息 */
    @Schema(description = "请求信息说明")
    private String message = "请求成功";
    /** 返回的数据 */
    @Schema(description = "请求数据")
    private T data;
    /** 返回的异常信息 */
    @Schema(description = "请求异常说明")
    private String error;

    // 静态构建器方法
    public static <T> ResultInfoBuilder<T> builder() {
        return new ResultInfoBuilder<T>();
    }

    // 构建器类
    public static class ResultInfoBuilder<T> {
        private Integer code = 200;
        private boolean result = true;
        private String message = "请求成功";
        private T data;
        private String error;

        public ResultInfoBuilder<T> code(Integer code) {
            this.code = code;
            return this;
        }

        public ResultInfoBuilder<T> result(boolean result) {
            this.result = result;
            return this;
        }

        public ResultInfoBuilder<T> message(String message) {
            int i = message.indexOf("{}");
            if (i == -1) {
                this.message = message;
            } else {
                message(message, this.data);
            }
            return this;
        }

        public ResultInfoBuilder<T> message(String message, Object... args) {
            StringBuilder result = new StringBuilder();
            int start = 0;
            int argIndex = 0;
            // 遍历模板寻找占位符{}
            for (int i = 0; i < message.length(); i++) {
                if (i < message.length() - 1
                        && message.charAt(i) == '{'
                        && message.charAt(i + 1) == '}') {
                    // 添加占位符前的文本
                    result.append(message, start, i);
                    // 添加参数（确保不越界）
                    if (argIndex < args.length) {
                        result.append(args[argIndex++]);
                    } else {
                        result.append("{}"); // 保留多余占位符
                    }
                    // 跳过占位符
                    i++;
                    start = i + 1;
                }
            }
            // 添加剩余文本
            result.append(message.substring(start));
            this.message = result.toString();
            return this;
        }

        public ResultInfoBuilder<T> data(T data) {
            this.data = data;
            return this;
        }

        public ResultInfoBuilder<T> error(String error) {
            this.error = error;
            return this;
        }

        // 构建方法
        public ResultInfo<T> build() {
            return new ResultInfo<>(code, result, message, data, error);
        }
    }
}

